# Copyright (c) 2025 Li Auto Inc. and its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# binary-tasking.mk
#

define transform-o-to-executable
@mkdir -p $(dir $@)
@echo "LD	$@"
$(hide)echo $(PRIVATE_ALL_OBJS) $(PRODUCT_EXTRA_OBJS) > $@.objlist
$(hide)$(CC)								\
        $(filter-out -l%, $(PRIVATE_LFLAGS))				\
        $(if $(strip $(PRIVATE_LDS)), --lsl-file=$(PRIVATE_LDS),)	\
        -f $@.objlist							\
        $(addprefix -Wl--whole-archive=,				\
			$(PRIVATE_LIBRARY) $(PRODUCT_EXTRA_ARCHIVES))	\
        $(filter -l%, $(PRIVATE_LFLAGS))				\
        -Wl--output=$@.hex:IHEX						\
        -Wl--map-file=$@.map						\
        -o $@
$(transform-o-to-executable-post)
endef

define transform-o-to-static-lib
@mkdir -p $(dir $@)
@echo "AR	$@"
$(hide)$(AR) -r $@ $(PRIVATE_ALL_OBJS)
endef

ifeq ($(filter linux, $(subst -, ,$(MAKE_HOST))),)
define dep_post_process
    $(hide)sed -i 's/"//g' $(patsubst %.o,%.d,$@)
endef
endif

define transform-c-to-o
@mkdir -p $(dir $@)
@echo "CC	$<"
$(hide)$(CC) $(c-includes) $(PRIVATE_CFLAGS)	\
       --make-target=$@				\
       --dep-file=$(patsubst %.o,%.d,$@)	\
       --create=object $< -o $@
$(dep_post_process)
endef

define transform-c-to-cmd
@echo "$@:$<" > $(patsubst %.o,%.cmd,$@)
@echo "$(CC) $(c-includes) $(PRIVATE_CFLAGS) --create $< -o $@" | tr '\n' ' ' | sed 's/\s\+/ /g' >> $(patsubst %.o,%.cmd,$@)
endef

# vim:ft=make
#
